package guan.controller;

import guan.bean.Book;
import guan.core.bean.Page;
import guan.dao.BookDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class BookController {
	@Autowired
	private BookDao bookDao;

	@RequestMapping(value = "book", method = RequestMethod.GET)
	public String book() {
		return "book";
	}

	@RequestMapping(value = "book-save", method = RequestMethod.POST)
	public String save(@ModelAttribute Book book) {
		bookDao.create(book);
		return "book";
	}

	@RequestMapping(value = "book?{id}", method = RequestMethod.GET)
	public String getById(Long id) {
		return "book";
	}

	@RequestMapping(value = "book-page", method = RequestMethod.GET)
	public String page(@ModelAttribute Page page, @ModelAttribute String like) {
		page.setCount(bookDao.countBooks(like));
		page.setList(bookDao.page(page.getPageNum() * page.getPageSize(),
				(page.getPageNum() + 1) * page.getPageSize(), like));
		return "book";
	}
}
